
/**
** \file       exec_andor.c
**
** \brief      this function execute and_or rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse, De Olivera
**             Guillaume and Nguyen Viet Dung
**
** \version    1.0
**
** \date       23 November 2010
**
*/

#include "execution.h"

static int      aux(struct andor        *node,
                    int                 ret)
{
  if (node->and_or == NULL)
    return (exec_pipeline(node->pl));

  if (strcmp(node->and_or, "&&") == 0)
  {
    ret = exec_pipeline(node->pl);

    if (ret == 1)
      return (1);
    else
      return (exec_pipeline(node->pl->next));
  }

  if (!strcmp(node->and_or, "||"))
  {
    ret = exec_pipeline(node->pl);
    if (ret == 0)
      return (0);
    else
      return (exec_pipeline(node->pl->next));
  }

  return (1);
}

int             exec_andor(struct andor *node)
{
  int           ret = 0;
  struct andor  *tmp_andor = NULL;

  if (node == NULL)
    return (0);

  if (node->pl == NULL)
  {
    free_andor(node);
    return (0);
  }

  if (node->next != NULL)
  {
    tmp_andor = node->next;
    node->next = NULL;

    ret = exec_andor(node);

    if (ret == 0)
      return (exec_andor(tmp_andor));

    return (1);
  }

  return (aux(node, ret));
}
